﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Linq;
using System.Web.Mvc;
using System.Web.UI;

using ProjectBase.Core;
using ProjectBase.Core.Web;
using ProjectBase.Mvc;
using ProjectBase.Mvc.Models;
using ProjectBase.Mvc.Controllers;

using Survey.Common;
using Survey.Common.Business.Services;
using Survey.Common.Models;
using Survey.Common.Controllers;
using Survey.Filters;

namespace Survey.Mvc.Controllers
{
    public class LayoutController : WebController<ILayoutModel>, ILayoutController
    {
        #region Keep session open
        /// <summary>
        ///     Allows the session to survive until the browser is closed.
        ///     There are calls from ProjectBase.js method called:
        ///     Sess__Request_SessionKeeper.
        ///     To allow Session keeper set web.config setting:
        ///     EnableSessionKeeper = "true"
        /// </summary>
        /// <returns>EmptyResult is a smart way how to inform the OnResult filters that no MasterPage is used</returns>
        public virtual string KeepSessionOpen()
        {
            return EmptyTag;
        }
        #endregion Keep session open

        #region CultureSettings
        [OutputCache(Location = OutputCacheLocation.Server, Duration = 600)]
        public virtual ActionResult CultureSettings(string id)
        {
            ViewData["cultureName"] = id;
            return View(Str.WebControls.CultureSettings);
        }

        #endregion CultureSettings

        #region Main TreeView
        public virtual ActionResult MainTV()
        {
            BindModel(Model.TreeViewModel);
            Model.TreeViewModel.CurrentItem = NodeFacade.GetNode(Model.TreeViewModel.NodeID);

            return View(new TreeViewNode(), Model.TreeViewModel);
        }

        public virtual ActionResult MainTreeView()
        {
            Model.TreeViewModel.DataSource = NodeFacade.GetRootNodes();

            return PartialView(Str.WebControls.MainTreeBlock, Model);
        }
        #endregion Main TreeView

        #region Menu TreeView
        public virtual ActionResult MenuTV()
        {
            BindModel(Model.TreeViewModel);

            Model.TreeViewModel.CurrentItem = SecurityManager.GrantedMenuNodes
                .Where(mi => mi.ID.Equals(Model.TreeViewModel.NodeID))
                .FirstOrDefault();

            return View(new TreeViewNode(), Model.TreeViewModel);
        }
        public virtual ActionResult MenuTreeView()
        {
            Model.TreeViewModel.DataSource = SecurityManager
                .GrantedMenuNodes
                .Where(mi => mi.ParentId.IsNull())
                .ToList();

            Model.TreeViewModel.ExpandedNodes = SecurityManager
                .ExpandedMenuNodes
                .ToList();
            return PartialView(Str.WebControls.MenuTreeBlock, Model);
        }
        #endregion Menu TreeView

        #region UserInfo
        public virtual ActionResult UserInfo()
        {
            SetCurrentUserModel(Model.CurrentUserModel);
            return PartialView(Str.WebControls.UserInfoBlock, Model.CurrentUserModel);
        }

        protected virtual void SetCurrentUserModel(ICurrentUserModel model)
        {
            SetUserInitials(model);
            if (!System.Threading.Thread.CurrentPrincipal.Identity.IsAuthenticated)
            {
                model.CurrentStateDescription = Str.Business.Services.Logon;
                model.CurrentStateEntity = Str.Business.Services.Logon;
                model.ChangeStateUrl = Url.Action(Str.Business.Services.Logon, Str.Controllers.Membership);
            }
            else if (Global.Settings.UseCurrentRole)
            {
                model.CurrentStateDescription = "Role";
                model.CurrentStateEntity = SecurityManager.User.CurrentRole;
                model.ChangeStateUrl = Url.Action(Str.Business.Services.SelectRole,
                    Str.Controllers.Membership);
            }

            if (SecurityManager.Identity.IsAuthenticated
                && SecurityManager.IsAuthenticationModeForms)
            {
                model.LogoutUrl = Url.Action(Str.Business.Services.Logoff, Str.Controllers.Membership);
            }
        }

        protected static void SetUserInitials(ICurrentUserModel model)
        {
            model.UserLogin = SecurityManager.UserLogin;
            model.UserName = SecurityManager.User.FirstName + " " + SecurityManager.User.LastName;
            if (SecurityManager.User.LastName.StartsWith("uid=", StringComparison.OrdinalIgnoreCase))
            {
                model.UserName = SecurityManager.User
                    .LastName
                    .Split(';')[0]
                    .Replace("uid=", string.Empty);
            }

        }
        #endregion UserInfo

        #region MasterPage selectors
        /// <summary> Renders icons and links to change MasterPage (set via AOP)</summary>
        public virtual ActionResult MasterPageSelectors()
        {
            return PartialView(Str.WebControls.MasterSelector, Model);
        }
        #endregion MasterPage selectors

        #region properties
        public override string ControllerName
        {
            get { return Str.Controllers.Layout; }
        }

        public virtual IStoryNodeFacade NodeFacade { protected get; set; }
        #endregion properties
    }
}
